package com.example.fin;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.fin.bean.FinItem;

import java.util.ArrayList;
import java.util.List;

public class DataSource extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "Student.db";
    public static final String TABLE_NAME = "student_table";
    private int size = -1;
    private List<FinItem> list = new ArrayList<>();

    public DataSource(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME + " " +
                "(id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "fee int," +
                "amount INTEGER," +
                "date varchar(20)," +
                "user varchar(50)," +
                "remark varchar(200)," +
                "mark integer default 0" +
                ")");
        db.execSQL("insert into "+TABLE_NAME +"(fee,amount,date,user,remark,mark) values (1,2,'2003-11-31','zhiwen','deposit','deposit $1')");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    public boolean insertData(FinItem item) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("fee", item.getFee());
        contentValues.put("amount", item.getAmount());
        contentValues.put("date", item.getDate());
        contentValues.put("remark", item.getRemark());
        contentValues.put("mark", item.isMark() ? 1 : 0);
        contentValues.put("user", item.getUser());
        long result = db.insert(TABLE_NAME, null, contentValues);
        if (result == -1)
            return false;
        else
            return true;
    }

    public List<FinItem> list(String user) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res = db.rawQuery("select * from " + TABLE_NAME + " where user= ?", new String[]{user});
        list.clear();
        while (res.moveToNext()) {
            list.add(getItem(res));
        }
        res.close();
        size = list.size();
        return list;
    }

    public FinItem getData(int pos) {
        return list.get(pos);
    }

    public FinItem getDetail(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
        FinItem item = new FinItem();
        if (res.moveToNext()) {
            item = getItem(res);
        }
        res.close();
        return item;
    }

    private static FinItem getItem(Cursor res) {
        FinItem item = new FinItem();
        item.setUser(res.getExtras().getString("user"));
        item.setFee(res.getExtras().getInt("fee"));
        item.setAmount(res.getExtras().getInt("amount"));
        item.setRemark(res.getExtras().getString("remark"));
        item.setDate(res.getExtras().getString("date"));
        item.setId(res.getExtras().getInt("id"));
        item.setMark(res.getExtras().getInt("mark") == 1);
        return item;
    }

    public boolean updateData(FinItem item) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("fee", item.getFee());
        contentValues.put("amount", item.getAmount());
        contentValues.put("date", item.getDate());
        contentValues.put("remark", item.getRemark());
        contentValues.put("mark", item.isMark() ? 1 : 0);
        contentValues.put("user", item.getUser());
        int update = db.update(TABLE_NAME, contentValues, "ID = ?", new String[]{"" + item.getId()});
        return update == 1;
    }

    public Integer deleteData(String id) {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete(TABLE_NAME, "ID = ?", new String[]{id});
    }

    public int getSize() {
        return size;
    }
}

